package com.mlnx.ui.service.impl;

import com.mlnx.service.base.service.BaseService;
import com.mlnx.service.common.enums.ResponseEnum;
import com.mlnx.service.common.exception.MutilDeviceServiceException;
import com.mlnx.ui.dao.WalkReportMapper;
import com.mlnx.ui.entity.LineData;
import com.mlnx.ui.entity.WalkReport;
import com.mlnx.ui.pojo.form.WalkReportForm;
import com.mlnx.ui.pojo.vo.WalkReportVO;
import com.mlnx.ui.service.LineDataService;
import com.mlnx.ui.service.WalkReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class WalkReportServiceImpl extends BaseService<WalkReportMapper, WalkReport> implements WalkReportService {

    @Autowired
    @Override
    public void setBaseMapper(WalkReportMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Autowired
    private LineDataService lineDataService;

    @Override
    public List<WalkReportVO> getSixWalkReports(Integer patientId) {
        return baseMapper.selectSixWalkReportsByPatientId(patientId);
    }

    @Override
    public WalkReportVO getSixWalkReport(Integer id) {
        return baseMapper.selectSixWalkReportsById(id);
    }

    @Override
    @Transactional
    public void saveReport(WalkReportForm walkReportForm) {
        WalkReport walkReport = new WalkReport();
        walkReport.setMeters(walkReportForm.getMeters());
        walkReport.setCircles(walkReportForm.getCircles());
        if (walkReportForm.getDistance() != null) {
            walkReport.setDistance(walkReportForm.getDistance());
        }
        if (walkReportForm.getRemark() != null) {
            walkReport.setRemark(walkReportForm.getRemark());
        }
        if (walkReportForm.getRemark2() != null) {
            walkReport.setRemark2(walkReportForm.getRemark2());
        }
        if (walkReportForm.getBorgLeg().getRank() != null) {
            walkReport.setBorglegRank(walkReportForm.getBorgLeg().getRank());
        }
        if (walkReportForm.getBorgLeg().getText() != null) {
            walkReport.setBorglegText(walkReportForm.getBorgLeg().getText());
        }
        if (walkReportForm.getBorgBreath() != null) {
            if (walkReportForm.getBorgBreath().getRank() != null) {
                walkReport.setBorgbreathRank(walkReportForm.getBorgBreath()
                        .getRank());
            }
            if (walkReportForm.getBorgBreath().getText() != null) {
                walkReport.setBorgbreathText(walkReportForm.getBorgBreath()
                        .getText());
            }
        }
        walkReport.setPatientId(walkReportForm.getPatientId());
        walkReport.setTimestamp(new Date());

        if (walkReportForm.getStartBp() != null) {
            if (walkReportForm.getStartBp().getDbp() != 0) {
                walkReport.setStartDbp(walkReportForm.getStartBp().getDbp());
            }
            if (walkReportForm.getStartBp().getSbp() != 0) {
                walkReport.setStartSbp(walkReportForm.getStartBp().getSbp());
            }
        }
        if (walkReportForm.getStopBp() != null) {
            if (walkReportForm.getStopBp().getDbp() != 0) {
                walkReport.setStopDbp(walkReportForm.getStopBp().getDbp());
            }
            if (walkReportForm.getStopBp().getSbp() != 0) {
                walkReport.setStopSbp(walkReportForm.getStopBp().getSbp());
            }
        }
        if (walkReportForm.getMET() != null) {
            walkReport.setMet(walkReportForm.getMET());
        }
        if (walkReportForm.getHeartLung() != null) {
            walkReport.setHeartLung(walkReportForm.getHeartLung());
        }

        save(walkReport);

        List<LineData> dataList = walkReportForm.getLineDatas();
        for (LineData data : dataList) {
            data.setWalkId(walkReport.getId());
        }

        try {
            lineDataService.saveBatch(LineData.class, dataList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MutilDeviceServiceException(ResponseEnum.ServerError);
        }
    }
}
